package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("users")
public class UserInfoController {
    @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.appkey}")
    private String appkey;

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/get")
    public Object get(String id){
        return userInfoService.get(id);
    }
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean byPhone = userInfoService.checkPhone(phone);
        return !byPhone;
    }

    //发送短信
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone,smsUrl,appkey);
        return new JsonResult();
    }

    //注册
    @PostMapping("/regist")
    public Object regist(String phone,String nickname,String password,String rpassword,String verifyCode) {
        userInfoService.regist(phone,nickname,password,rpassword,verifyCode);

        return JsonResult.success();
    }

    //登录
    @PostMapping("/login")
    public Object login(String username,String password) {
        //登录成功返回用户对象
        UserInfo user = userInfoService.login(username,password);

        //创建token,将token添加到redis中
        String token = userInfoRedisService.setToken(user);

        //user token对象
        HashMap<String, Object> map = new HashMap<>();
        map.put("user",user);
        map.put("token",token);


        return JsonResult.success(map);
    }

    @RequiredLogin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request) {
        //获取请求头上的token
        String token = request.getHeader("token");
        //通过token从redis中获取用户信息
        UserInfo user = userInfoRedisService.getUserByToken(token);

        System.out.println(user);
        return JsonResult.success(user);
    }

    //收藏攻略
    @GetMapping("/strategies/favor")
    public Object favor(String userId){
        //创建一个空的list集合
//        List<String> list = Collections.emptyList();
        //判断是否登录
//        if (StringUtils.hasLength(userId)){
            List<String> list = strategyStatisVORedisService.getUserStrategyFavor(userId);
//        }
        return JsonResult.success(list);
    }

}
